package com.usian.admin.controller.v1;

import com.usian.admin.service.AdChannelService;
import com.usian.apis.admin.AdChannelControllerApi;
import com.usian.model.admin.pojos.AdUser;
import com.usian.utils.common.exception.CatchCustomException;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.threadlocal.AdminThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/channel")

public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService adChannelService;


    /**
     *
     * @param channelDto
     * @return
     */
    @Override
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto channelDto) {
        //获取dto参数  1. url方式传值  2.表单方式 queryParam方式 3.Json方式传值  AdminThreadLocalUtils
        AdUser aduser =  AdminThreadLocalUtils.getUser();
        if (aduser==null){
            CatchCustomException.catchs(20002,"用户不合法，没有登录");
        }

        return adChannelService.findByNameAndPage(channelDto);
    }

    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdChannel adChannel) {
        return adChannelService.save(adChannel);
    }

    @Override
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id) {
        return adChannelService.findById(id);
    }

    @Override
    @PutMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return adChannelService.update(adChannel);
    }

    @Override
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return adChannelService.delete(id);
    }

    @Override
    @DeleteMapping("/deletes")
    public ResponseResult deletes(@RequestBody Integer[] ids) {
        return adChannelService.deletes(ids);
    }

    @Override
    @GetMapping("/channels")
    public ResponseResult findAll() {
        return adChannelService.findAll();
    }


}
